(function(angular) {
	"use strict";
	angular.module("fs.waf").factory("settlePaySwiftService", settlePaySwiftService);
	settlePaySwiftService.$inject = [ "commonService", "addressInfoService", "paySwiftService" ];
	function settlePaySwiftService(commonService, addressInfoService, paySwiftService) {

		function initPayment(settlement, layoutConfig, payDetail) {
			var payPartyConfig = null;
			payDetail.deliveryType = "W";
			if (payDetail.payCodeObj && payDetail.paymentData) {
				if (!payDetail.paymentData.payParties) {
					payDetail.paymentData.payParties = [];
				}
				var swiftObj = payDetail.paymentData;
				if (swiftObj.caseCode && swiftObj.payswiftType) {
					payPartyConfig = doSwiftTypeCallBack(settlement, swiftObj, undefined, layoutConfig);
				}
			}
			return payPartyConfig;
		}

		function doSwiftTypeCallBack(settlement, swiftObj, swiftCase, layoutConfig) {
			if (swiftCase) {
				swiftObj.caseCode = swiftCase.caseCode;
				swiftObj.payswiftType = swiftCase.swiftType;
			}
			angular.forEach(settlement.swiftCaseList, function(data, index, array) {
				if (data.caseCode == swiftObj.caseCode && data.swiftType == swiftObj.payswiftType) {
					swiftObj.hasCoverMsg = data.globalSwiftCase.hasCoverMsg;
					if (!swiftObj.coverMsgReceiver || !commonService.isValidOption(settlement.allPartyTypeOptions, swiftObj.coverMsgReceiver)) {
						swiftObj.coverMsgReceiver = data.globalSwiftCase.coverMsgReceiver;
					}
					if (swiftObj.hasCoverMsg == "N") {
						swiftObj.coverMsgReceiver = "";
					}
				}
			});
			// displaySwiftField(swiftObj);
			return initPayPartyTabs(swiftObj, settlement, layoutConfig);
		}
		
		function initPayPartyTabs(swiftObj, settlement, layoutConfig){
			swiftObj.payPartyTabs = [];
			var payPartyConfig = [], caseCodes = [];
			if (swiftObj.caseCode) {
				if (swiftObj.caseCode.indexOf(";") != -1) {
					caseCodes = swiftObj.caseCode.split(";");
				} else {
					caseCodes[0] = swiftObj.caseCode;
				}
			}
			caseCodes.push("SI");
			angular.forEach(caseCodes, function(caseCode, index, array) {
				angular.forEach(settlement.allPartyTypeOptions, function(allPaypartype, key) {
					if (allPaypartype.value == caseCode) {
						swiftObj.payPartyTabs.push(allPaypartype);
					}
				});
			});
			// settlement.coverMsgReceiverOptions = swiftObj.payPartyTabs;
			settlement.coverMsgReceiverOptions = settlement.allPartyTypeOptions;
			if (!swiftObj.payParties) {
				swiftObj.payParties = [];
			}
			swiftObj.payPartiesCache = swiftObj.payPartiesCache || {};
			angular.forEach(swiftObj.payPartyTabs, function(data, index, array) {
				swiftObj.payParties[index] = swiftObj.payPartiesCache[data.value] || swiftObj.payParties[index] || {};
				swiftObj.payPartiesCache[data.value] = swiftObj.payParties[index];
				swiftObj.payParties[index].partyType = data.value;
				payPartyConfig[index] = angular.copy(layoutConfig.formConfigs.payParty);
			});
			return payPartyConfig;
		}
		
		function getPayPartyConfigs(settlement, layoutConfig){
			var payPartyConfig = [];
			angular.forEach(settlement.payDetails, function(payDetail, index, array) {
				if (payDetail.paymentData && payDetail.paymentData && payDetail.paymentData.payParties) {
					angular.forEach(payDetail.paymentData.payPartyTabs, function(payParty, item) {
						payPartyConfig["pd" + payDetail.payItemSeq] = payPartyConfig["pd" + payDetail.payItemSeq] || [];
						payPartyConfig["pd" + payDetail.payItemSeq][item] = angular.copy(layoutConfig.formConfigs.payParty);
					});
				}
			});
			return payPartyConfig;
		}

		function displaySwiftField(swiftObj) {
			if (swiftObj && swiftObj.numParties && swiftObj.reveiverParty) {
				var mapToSwift = paySwiftService.addSwiftFieldTip({
					numOfParty : swiftObj.numParties,
					receiverOfMT103 : swiftObj.reveiverParty
				});
				swiftObj.mapToSwift = mapToSwift.mapToSwift;
			}
		}

		function swiftTypeCallBack(settlement, layoutConfig, result) {
			var swiftObj = result.swiftObj;
			var swiftCase = result.swift;
			return doSwiftTypeCallBack(settlement, swiftObj, swiftCase, layoutConfig);
		}

		function partyCountryChanged(settlement, payParty) {
			// var partyStates =
			// addressInfoService.getStateOptions(settlement.stateList,
			// payParty.partyCountry);
			// var partyCitys =
			// addressInfoService.getCityOptions(settlement.cityList,
			// payParty.partyCountry, payParty.partyState);
			// if (partyStates && partyStates.length > 0) {
			// settlement.partyStates = partyStates;
			// } else {
			// settlement.partyStates = settlement.partyStatesCatch;
			// }
			// if (partyCitys && partyCitys.length > 0) {
			// settlement.partyCitys = partyCitys;
			// } else {
			// settlement.partyCitys = settlement.partyCitysCatch;
			// }
		}

		function partyStateChanged(settlement, payParty) {
			// var partyCitys =
			// addressInfoService.getCityOptions(settlement.cityList,
			// payParty.partyCountry, payParty.partyState);
			// if (partyCitys && partyCitys.length > 0) {
			// settlement.partyCitys = partyCitys;
			// } else {
			// settlement.partyCitys = settlement.partyCitysCatch;
			// }
		}

		function getPaymentInfoConfig(payDetail, paymentInfoConfig) {
			var dynaConfig = angular.copy(paymentInfoConfig);
			dynaConfig.gridConfigs.coverMsgReceiver.show = "vm.settlement.payDetailCache.pd" + payDetail.payItemSeq + ".paymentData[0].hasCoverMsg == 'Y'";
			return dynaConfig;
		}

		function getOtherInfoConfig(payDetail, layoutConfigConfig) {
			var dynaConfig = angular.copy(layoutConfigConfig);
			dynaConfig.gridConfigs.orderedAmt.format = "vm.settlement.payDetailCache.pd" + payDetail.payItemSeq + ".paymentData[0].orderedCcy";
			dynaConfig.gridConfigs.senderChrgAmt.format = "vm.settlement.payDetailCache.pd" + payDetail.payItemSeq + ".paymentData[0].senderChrgCcy";
			dynaConfig.gridConfigs.recvrChrgAmt.format = "vm.settlement.payDetailCache.pd" + payDetail.payItemSeq + ".paymentData[0].recvrChrgCcy";
			return dynaConfig;
		}

		function getDeliveryTypeConfig(payDetail, deliveryTypeConfig) {
			var dynaConfig = angular.copy(deliveryTypeConfig);
			if (payDetail.payCodeObj && payDetail.payCodeObj.paycodeCat.substr(7) == "SWIFT") {
				dynaConfig.gridConfigs.deliveryType.options = {
					"data" : [ {
						"label" : "label.settlement.swift",
						"value" : "W"
					} ]
				};
			}
			return dynaConfig;
		}

		function checkpayParties(payDetail) {
			if (payDetail.payCodeObj && payDetail.paymentData) {
				var swiftObj = payDetail.paymentData;
				if (swiftObj.payPartyTabs) {
					swiftObj.payParties.splice(swiftObj.payPartyTabs.length);
				}
			}
		}

		function getPayInfoConfig(payDetail, result, layoutConfig) {
			result.deliveryTypeConfig = result.deliveryTypeConfig || [];
			result.paymentInfoConfig = result.paymentInfoConfig || [];
			result.senderToReceiverInfoConfig = result.senderToReceiverInfoConfig || [];
			result.otherInfoConfig = result.otherInfoConfig || [];
			result.deliveryTypeConfig["pd" + payDetail.payItemSeq] = getDeliveryTypeConfig(payDetail, layoutConfig.formConfigs.deliveryType);
			result.paymentInfoConfig["pd" + payDetail.payItemSeq] = getPaymentInfoConfig(payDetail, layoutConfig.formConfigs.paymentInfo);
			result.senderToReceiverInfoConfig["pd" + payDetail.payItemSeq] = angular.copy(layoutConfig.formConfigs.senderToReceiverInfo);
			result.otherInfoConfig["pd" + payDetail.payItemSeq] = getOtherInfoConfig(payDetail, layoutConfig.formConfigs.otherInfo);
		}

		return {
			initPayment : initPayment,
			initPayPartyTabs : initPayPartyTabs,
			swiftTypeCallBack : swiftTypeCallBack,
			partyCountryChanged : partyCountryChanged,
			partyStateChanged : partyStateChanged,
			checkpayParties : checkpayParties,
			getPayInfoConfig : getPayInfoConfig,
			getPayPartyConfigs : getPayPartyConfigs
		};
	}
})(angular);
